import { RedisClientType, RedisDefaultModules, createClient } from 'redis'

let _client: RedisClientType

export async function client(): Promise<RedisClientType> {
  if (_client === undefined) {
    _client = await createClient<RedisDefaultModules, Record<string, never>, Record<string, never>>()
      .on('error', (error) => console.log('Redis Client Error', error))
      .connect()
  }
  return _client
}

export type GetRedisClient = typeof client
